//
//  GuiderListVC.m
//  ImGuider
//
//  Created by 李陆涛 on 2017/12/26.
//  Copyright © 2017年 imguider. All rights reserved.
//

#import "GuiderListVC.h"
#import "GuiderNodeCell.h"
#import "GuiderDetailVC.h"

@interface GuiderListVC ()

@property (nonatomic, strong) NSArray *dataSource;

@end

@implementation GuiderListVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = MYLocalizedString(@"HOMEPAGE_GUIDER", nil);
    
    [self registCells:@[@"GuiderNodeCell"]];
    [self setAutoRowHeight];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    self.view.backgroundColor = kBGColor;
    self.tableView.backgroundColor = kBGColor;
    
    [self setupData];
}

- (void)setupData {
    
    [ActivityIndicatorView showHUDAddedTo:self.view];
    if (!self.city) {
        
        [NetAPIManager getAllGuidersBlock:^(NSArray *guiderArray, NSError *error) {
            
            [ActivityIndicatorView hideHUDAddedTo:self.view];
            self.dataSource = guiderArray;
            [self.tableView reloadData];
        }];
    } else {
        
        [NetAPIManager getAllGuiderBy:self.city.cid start:@(0) count:@(10000) block:^(NSArray *guidersArray, NSError *error) {
            
            [ActivityIndicatorView hideHUDAddedTo:self.view];
            self.dataSource = guidersArray;
            [self.tableView reloadData];
        }];
    }
}

#pragma mark - Table view data source

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.dataSource.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    GuiderModel *model = self.dataSource[indexPath.row];
    
    GuiderNodeCell *cell = [tableView dequeueReusableCellWithIdentifier:@"GuiderNodeCell"];
    cell.model = model;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    GuiderModel *model = self.dataSource[indexPath.row];
    
    GuiderDetailVC *detailvc = [GuiderDetailVC new];
    detailvc.guiderID = model.guideid;
    detailvc.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:detailvc animated:YES];
}

@end

